// index.js

Page({
  data: {
    fileList: [
      {
        url: 'https://img.yzcdn.cn/vant/leaf.jpg',
        name: '图片1',
        deletable:false
      },
      // Uploader 根据文件后缀来判断是否为图片文件
      // 如果图片 URL 中不包含类型信息，可以添加 isImage 标记来声明
      {
        url: 'http://iph.href.lu/60x60?text=default',
        name: '图片2',
        isImage: true,
        deletable: true,
      },
    ],
    typeList:[],
    mainActiveIndex: 0,
    activeId: null,
    theUrl:'',
    fileList1:[],
    fileIDs1:[],
    fileList1:[],
    fileIDs1:[],
    fileList2:[],
    fileIDs2:[],
    fileList3:[],
    fileIDs3:[],
    fileList4:[],
    fileIDs4:[],
    bigType:'',
    smallType:'',
    detailArray:[],
    detailSwiper:[],
    name:'',
    price:'',
    select1:[],
    select1Name:'',
    select2:[],
    select2Name:'',
    shopid:2,
    url:''
  },
  thisInsert(e){
    let {type}  = e.mark
    this.setData({
      [type]:e.detail.value
    })
    console.log(e)
  },
  // 大类
  onClickNav({ detail = {} }) {
    let {typeList} = this.data
    this.setData({
      mainActiveIndex: detail.index || 0,
      theUrl:typeList[detail.index].url,
      bigType:typeList[detail.index].text
    });
  },
// 小类
  onClickItem({ detail = {} }) {
    const activeId = this.data.activeId === detail.id ? null : detail.id;
    this.setData({ 
      activeId,
      smallType:detail.text 
    });
  },
// 上传首页图片
  async afterRead1(event) {
    const { file } = event.detail;
    console.log(file)
    
      let res = await wx.cloud.uploadFile({
        cloudPath: Date.now()+'.png',
        filePath:file.tempFilePath
      })
      let res2 = await wx.cloud.getTempFileURL({
        fileList:[res.fileID]
      })
      this.data.fileIDs1.push({
        url:res2.fileList[0].tempFileURL
      })
      this.data.fileList1.push({
        url:res2.fileList[0].tempFileURL
      })
      this.setData({
        fileList1:this.data.fileList1,
        fileIDs1:this.data.fileIDs1
      })
  },

// 上传规格图片,颜色
  async afterRead2(event) {
    const { file } = event.detail;
    console.log(file)
    file.forEach(async (item,index)=>{
      let res = await wx.cloud.uploadFile({
        cloudPath: Date.now()+index+'.png',
        filePath:item.tempFilePath
      })
      let res2 = await wx.cloud.getTempFileURL({
        fileList:[res.fileID]
      })
      this.data.fileIDs2.push({
        url:res2.fileList[0].tempFileURL
      })
      this.data.fileList2.push({
        url:res2.fileList[0].tempFileURL
      })
      this.setData({
        fileList2:this.data.fileList2,
        fileIDs2:this.data.fileIDs2
      })
    })
  },



// 商品详情页下方图片
  async afterRead3(event) {
    const { file } = event.detail;
    console.log(file)
    file.forEach(async (item,index)=>{
      let res = await wx.cloud.uploadFile({
        cloudPath: Date.now()+index+'.png',
        filePath:item.tempFilePath
      })
      let res2 = await wx.cloud.getTempFileURL({
        fileList:[res.fileID]
      })
      this.data.fileIDs3.push({
        url:res2.fileList[0].tempFileURL
      })
      this.data.fileList3.push({
        url:res2.fileList[0].tempFileURL
      })
      this.setData({
        fileList3:this.data.fileList3,
        fileIDs3:this.data.fileIDs3
      })
    })
  },
  // 上传商品详情轮图
  async afterRead4(event) {
    const { file } = event.detail;
    console.log(file)
    file.forEach(async (item,index)=>{
      let res = await wx.cloud.uploadFile({
        cloudPath: Date.now()+index+'.png',
        filePath:item.tempFilePath
      })
      let res2 = await wx.cloud.getTempFileURL({
        fileList:[res.fileID]
      })
      this.data.fileIDs4.push({
        url:res2.fileList[0].tempFileURL
      })
      this.data.fileList4.push({
        url:res2.fileList[0].tempFileURL
      })
      this.setData({
        fileList4:this.data.fileList4,
        fileIDs4:this.data.fileIDs4
      })
    })
  },

  async getClass(){
    let res = await wx.cloud.callFunction({
      name:"getSevenType"
    })
    if(res.result.code==200){
      this.setData({
        typeList:res.result.data.data
      })
    }
  },
  async uptest(){
    let {name}=this.data
    console.log(name);
    let res = await wx.cloud.callFunction({
      name:'test',
      data:{
        name
      }
    })
    console.log(res)
  },
  async upShop(){
    let {smallType,shopid,name,price,select1Name,select2Name,bigType,fileList1,fileList2,fileList3,fileList4,select1,select2,select1detail} = this.data
    console.log(this.data)
    // 小类价格
    let arr = select1.split(',')
    let Last = arr.map(item=>({price:parseInt(item)}))
    // 小类颜色
    let arr1 = select1detail.split(',')
    let last1 = arr1.map(item=>({name:item}))
    //小类图片 fileList2
    let theSelect = fileList2.map((el,index)=>{
      return {...el,...Last[index]}
    })
    // 最终结果,传入数据的select1
    let theSelect1 = theSelect.map((el,index)=>{
      return {...el,...last1[index]}
    })
    //最终结果,传入数据的detailArray
    let detailArray = [] 
    fileList3.map(item=>{
      detailArray.push(item.url)
    })
    //大小码处理 最终结果传入select2
    let arr2 = select2.split(',')
    let last2 = arr2.map(item=>({size:item}))
    console.log(smallType,
      detailArray,
      fileList4,
      name,
      price,
      theSelect1,
      select1Name,
      last2,
      select2Name,
      shopid,
      bigType,
      fileList1[0].url)
    let res = await wx.cloud.callFunction({
      name:'addSevenShopList',
      data:{
        detail:smallType,
        detailArray,
        detailSwiper:fileList4,
        name,
        price,
        select1:theSelect1,
        select1Name,
        select2:last2,
        select2Name,
        shopid,
        type:bigType,
        url:fileList1[0].url
      }
    })
    if(res.result.code==200){
      wx.showToast({
        title: res.result.msg,
        icon:'success'
      })
    }
  },
  onLoad(){
    setTimeout(()=>{
      this.getClass()
    },1000)
  }
})
